package de.bastian.hinterleitner.tools;

import java.util.Date;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Log;

public class ErrorLog {

	public static void write(Context context, String error) {
		Log.e("ERRORLOG", error);
		SharedPreferences sp = context.getSharedPreferences("values",
				context.MODE_PRIVATE);
		String log = sp.getString("log", "");
		log += new Date(System.currentTimeMillis()).toGMTString()+"\n";
		log += "\n" + error +"\n";
		sp.edit().putString("log", log).commit();
		save(context);
	}

	public static String read(Context context) {
		SharedPreferences sp = context.getSharedPreferences("values",
				context.MODE_PRIVATE);
		String log = sp.getString("log", "");
		return log;
	}

	public static void write(Context context, Exception e) {
		SharedPreferences sp = context.getSharedPreferences("values",
				context.MODE_PRIVATE);
		String log = sp.getString("log", "");
		log += new Date(System.currentTimeMillis()).toGMTString()+"\n";
		log += "\n" + e.getClass().toString()+": "+e.getMessage();
		for (StackTraceElement s : e.getStackTrace()) {
			log += "\n" + s.toString();
		}
		sp.edit().putString("log", log+"\n").commit();
		save(context);
	}

	private static void save(Context context){
		String log = read(context);
		final String path = Environment.getExternalStorageDirectory() + "/hijacksoft/watchyourcash/"
				+ "watchyourcash_errors.log";
		try {
			XmlHelper.save(log, path);
		} catch (Exception e) {

		}
	}
	
}
